package py.com.alfa.escriba.web.services.impl;

import java.util.List;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;

import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dao.VariablestemplatesDAO;
import py.com.alfa.escriba.persistence.dto.VariablestemplatesDTO;
import py.com.alfa.escriba.util.Utils;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.services.VariablestemplatesService;

public class VariablestemplatesServiceImpl implements VariablestemplatesService{

  private VariablestemplatesDAO variablestemplatesDAO;

  public void setVariablestemplatesDAO(VariablestemplatesDAO variablestemplatesDAO) {
    this.variablestemplatesDAO = variablestemplatesDAO;
  }

  @Override
  public List<VariablestemplatesDTO> listVariablestemplates(InvocationContext ic) throws ServiceException {
    List<VariablestemplatesDTO> variablestemplatess =  variablestemplatesDAO.listVariablestemplates();

    return variablestemplatess;
  }

  @Override
  public VariablestemplatesDTO getVariablestemplates(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    VariablestemplatesDTO variablestemplates =  variablestemplatesDAO.getVariablestemplatesById(id);

    if (variablestemplates == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"variablestemplates.notfound", id);

    return variablestemplates;
  }

  @Override
  public void deleteVariablestemplates(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    VariablestemplatesDTO variablestemplates =  variablestemplatesDAO.getVariablestemplatesById(id);

    if(variablestemplates == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"variablestemplates.notfound", id);

    try {
      variablestemplatesDAO.deleteVariablestemplates(variablestemplates);
    }
    catch (DataIntegrityViolationException e){
      throw new ServiceException(ErrorCode.DATA_INTEGRITY_VIOLATION, "variablestemplates.constraint.violation", id);
    }

  }

  @Override
  public VariablestemplatesDTO updateVariablestemplates(Long id, VariablestemplatesDTO dto, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    if (null == dto)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    VariablestemplatesDTO variablestemplates = variablestemplatesDAO.getVariablestemplatesById(id);

    if (variablestemplates == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"variablestemplates.notfound", id);

    variablestemplates.setIdtemplate(dto.getIdtemplate());
    variablestemplates.setIdvariable(dto.getIdvariable());
    variablestemplates.setActivo(dto.getActivo());
 

    Utils.validarDTO(dto);

    variablestemplatesDAO.updateVariablestemplates(dto);

    return variablestemplates;
  }

  @Override
  public VariablestemplatesDTO createVariablestemplates(VariablestemplatesDTO dto, InvocationContext ic) throws ServiceException {
    if (dto == null)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    try {

      Utils.validarDTO(dto);

      variablestemplatesDAO.createVariablestemplates(dto);
    }
    catch (DuplicateKeyException e){
      throw new ServiceException(ErrorCode.DUPLICATE_KEY_EXCEPTION, "variablestemplates.duplicate.key");
    }

    return dto;
  }

}